package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.article.service.GeneratePageService;
import com.heima.common.exception.CustException;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.enums.AppHttpCodeEnum;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class GeneratePageServiceImpl implements GeneratePageService {

    @Autowired
    Configuration configuration;
    @Autowired
    AuthorMapper authorMapper;
    @Autowired
    @Qualifier("minIOFileStorageService")
    FileStorageService fileStorageService;
    @Value("${file.minio.prefix}")
    private String prefix;
    @Autowired
    ApArticleMapper apArticleMapper;

    /**
     * 生成文章静态页面
     */
    @Override
    public void generateArticlePage(String content, ApArticle apArticle) {
        //1.获取文章内容
        //diaoqu内容
        try {
            //获取模板
            Template template = configuration.getTemplate("article.ftl");
            //2. 替换模板数据
            Map params =new HashMap<>();
            params.put("content", JSON.parseArray(content));
            params.put("article",apArticle);//文章信息
//            ApAuthor apAuthor = authorMapper.selectById(apArticle.getAuthorId());
//            params.put("authorApUserId",apAuthor.getUserId());
            ApAuthor author = authorMapper.selectById(apArticle.getAuthorId());
            params.put("authorApUserId", author.getUserId());// 作者 对应的 apUserId
            //基于数据 替换文本
            StringWriter writer =new StringWriter();
            template.process(params,writer);
            String htmlConcent = writer.toString();
            //得到输入流对象
            ByteArrayInputStream inputStream = new ByteArrayInputStream(htmlConcent.getBytes());
            // 将得到的输入流对象 上传到minio
            // 参数1 上传到哪个文件夹 参数2 文件名称 参数3 文件类型 参数4 输入流
            String store = fileStorageService.store(prefix, apArticle.getId() + ".html", "text/html", inputStream);
            //5. 将上唇成功的minio文件路径 设置ap_article中的static_url字段上
            apArticle.setStaticUrl(store);
            apArticleMapper.updateById(apArticle);
            log.info("成功生成静态页");

        } catch (IOException | TemplateException e) {
            e.printStackTrace();
            log.error("生成静态页失败");
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,"生产静态页面失败");

        }
    }
}
